package com.comando.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.comando.base.BaseAction;
import com.comando.dto.UserDto;
import com.comando.interceptor.LoginInterceptor;
import com.comando.pub.Json;
import com.comando.service.LoginService;
import com.comando.service.UserService;
import com.comando.util.CookieUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 登录ACTION
 * 
 */
@Namespace("/login")
@Action(value = "loginAction", results = {
		@Result(name = "logininput", location = "/login/login.jsp"),
		@Result(name = "success", location = "/login/success.jsp"),
		@Result(name = "fail", location = "/login/fail.jsp")}, 
		interceptorRefs={@InterceptorRef("defaultStack")})
public class LoginAction extends BaseAction implements ModelDriven<UserDto>,
		SessionAware, ServletRequestAware, ServletResponseAware {

	private static final Logger logger = Logger.getLogger(LoginAction.class);

	private Map<String, Object> mySession;
	private HttpServletResponse response; 
	private HttpServletRequest request; 

	private UserService userService;
	private LoginService loginService;

	@Override
	public void setSession(Map<String, Object> session) {
		this.mySession = session;

	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public LoginService getLoginService() {
		return loginService;
	}

	@Autowired
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	private UserDto userDto = new UserDto();

	public UserDto getModel() {
		return userDto;
	}

	public String logininput() {
		logger.info("进入logininput");
		return "logininput";
	}

	public void login() {
		Json j = new Json();
		UserDto u = loginService.login(userDto);
		if (u != null) {
			/*SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setUserId(u.getUserId().toString());
			sessionInfo.setLoginName(userDto.getLoginName());
			sessionInfo.setLoginPassword(userDto.getPassword());
			sessionInfo.setIp(ContextUtil.getIpAddr(ServletActionContext
					.getRequest()));*/
			
			//this.setSession((Map<String, Object>) new HashMap().put("sessionInfo", sessionInfo));
			//mySession.put("username", u.getLoginName());
			mySession.put("uid", u.getUserId().toString());
			
			//处理cookie
			//_qunawan记录登录用的用户名密码
			//_username记录登录后信息，的用户名
			String cookieValue = u.getLoginName() + ","	+ u.getPassword();
			String domain = request.getServerName();
			int cookieTime = 60 * 60 * 24 * 14;
			//logger.debug(host);
			CookieUtil.addCookie(response, "_qunawan", cookieValue, domain, cookieTime);
			CookieUtil.addCookie(response, "_username", u.getLoginName(), domain, -1);

			j.setSuccess(true);
			//j.setObj(sessionInfo);
			j.setMsg("登录成功！");
			
		} else {
			j.setSuccess(false);
			j.setMsg("登录失败！用户名或密码错误！");
		}
		super.writeJson(j);
	}
	
	public String logout() {
		HttpSession session = request.getSession(false);
		session.removeAttribute("uid");
		Cookie[] cookies = request.getCookies();
		String domain = request.getServerName();
		if (cookies!=null) {
			for (Cookie cookie : cookies) {
				if ("_qunawan".equals(cookie.getName())) {
					CookieUtil.addCookie(response, "_qunawan", "", domain, 0);
					//CookieUtil.delCookie(response, cookie);
	            }if ("_username".equals(cookie.getName())) {
					CookieUtil.addCookie(response, "_username", "", domain, 0);
					//CookieUtil.delCookie(response, cookie);
	            }
			}
		}
	
		return "login";
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response; 		
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}


}
